<template>
	<view class="jjnhnshgdd pd">
		<view class="cen fz36 cf">
			为您的爱车起个名字吧~
		</view>

		<input type="text" value="" class="jhhgerttyt dsfdstyser" placeholder="请输入" v-model="name"/>
	</view>
</template>
<script>
	var jsModule = uni.requireNativePlugin("MyModule")
	export default {
		data() {
			return {
				name:"",
				carform:{}
			}
		},
		components: {},
		methods: {
			async addcar(){
				console.log(this.name)
				console.log(this.carform)
				
				if(!this.name){
					uni.showToast({
						icon:"none",
						title:'请输入名字'
					})
					return
				}
				if(!this.carform.hasOwnProperty('image')){
					uni.showToast({
						icon:"none",
						title:'请上传图片'
					})
					return
				}
				if(this.carform.image.length == 0){
					uni.showToast({
						icon:"none",
						title:'请上传图片'
					})
					return
				}
				if(this.carform.engine.length == 0){
					delete this.carform.engine
				}
				if(this.carform.vin.length == 0){
					delete this.carform.vin
				}
				if(this.carform.year.length == 0){
					delete this.carform.year
				}
				console.log(this.carform)
				uni.showLoading()
				this.carform.name=this.name
				let params={}
				params=this.carform
				// this.carform.devsn = '-1789263741'
				var that =this
				if (!that.$store.state.isintcar && that.islogin()) {
					// 用户登录+车辆jar包初始化+绑定车辆
					uni.showToast({
						title: '初始化车辆' + that.islogin()
					})
					
					uni.$emit('carinit', {
						th: that,
						uid: that.islogin()
					}) 
				}
				uni.showLoading()
				setTimeout(function(){
					that.dqBindDevice(params.serial,function(e){
						if(e.success){
							let devsn = e.data.devsn
							let imei = e.data.imei
							params.devsn = devsn
							that.carform.devsn = devsn
							that.carform.network = imei
							let car=that.postcar()
							// that.$store.commit('setisintcar', false)
							setTimeout(function(){
								uni.hideLoading()
								uni.switchTab({
								    url: '/pages/index/index'
								});
							},1500)
						}else{
							uni.hideLoading()
							uni.showToast({
								title:e.message,
								icon:"none"
							})
						}
					}) 
				},2000)
				
				// console.log('开始存储'); 
				// that.postcar()
				// console.log('调用完毕'); 
				// console.log('调用完毕2'); 
			},
			async postcar(){
				// console.log('启动'); 
				 let res =await this.post('/api/car',this.carform)
				 // console.log(res);
				 // console.log('456789');
			}
		},
		onNavigationBarButtonTap(e) {
			// console.log(66666666666)
			this.addcar()
			// setTimeout(function(a){
			// 	uni.switchTab({
			// 	    url: '/pages/index/index'
			// 	});
			// },2000)
		},
		mounted() {},
		onLoad(options) {
			this.carform = JSON.parse(options.data)
			console.log(this.carform)
			// this.carform=uni.getStorageSync('carform')
		}
	}
</script>
<style scoped>
	.jjnhnshgdd {
		padding-top: 300upx;
	}

	.dsfdstyser {
		margin-top: 170upx;
	}
</style>
